'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	if (event.leixing == 1) {
		const $ = db.command.aggregate
		const res = await db.collection('gouwuche')
			.aggregate()
			 .match({  // 新增用户过滤条件 :ml-citation{ref="5" data="citationList"}
			        user_id: event.user_id 
			      })
			.lookup({
				from: 'goods', // 要联查的商品表
				localField: 'goods_id', // 主表关联字段
				foreignField: '_id', // 副表关联字段
				as: 'goods_info' // 查询结果存放字段
			})
			.addFields({
				goods_info: $.arrayElemAt(['$goods_info', 0]) // 转换数组为对象
			})
			.match({
				goods_info: $.neq(null) // 过滤无效商品数据
			})
			.project({
				_id: 1,
				goods_id: 1,
				color: 1,
				size: 1,
				count: 1,
				tupian_url: '$goods_info.tupian_url',
				goods_name: '$goods_info.goods_name',
				goods_price: '$goods_info.goods_price'
			})
			.end()
		return res
	}
};